package com.pig4cloud.pigx.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.mysql.utils.MysqlUtil;
import com.pig4cloud.pigx.common.customize.security.utils.SecurityUtil;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.common.seata.utils.SeataUtils;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.contract.entity.ContractOrder;
import com.pig4cloud.pigx.contract.mapper.ContractOrderMapper;
import com.pig4cloud.pigx.contract.service.TestService;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import com.pig4cloud.pigx.wallet.api.feign.WalletContractAccountControllerFeign;
import com.pig4cloud.pigx.wallet.dto.WalletContractAccountChangeDTO;
import com.pig4cloud.pigx.wallet.entity.ContractAccount;
import com.pig4cloud.pigx.wallet.enums.WalletContractAccountDetailEnum;
import io.seata.spring.annotation.GlobalLock;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 测试
 *
 * @since 2022/9/21 10:47
 */
@Service
@AllArgsConstructor
public class TestServiceImpl implements TestService {

	private final WalletContractAccountControllerFeign walletContractAccountControllerFeign;
	private final ContractOrderMapper contractOrderMapper;

	private final CommonUtil commonUtil;
	private final MysqlUtil mysqlUtil;
	private final ContractUtils contractUtils;

	/**
	 * 修改合约账户
	 *
	 * @param operationType 操作类型
	 * @since 2022/9/21 10:46
	 */
	@Override
	public void changeAsset(String operationType) {
		// 开启本地事务
		mysqlUtil.startLocalTx();

		// 锁定本地一条数据
//		QueryWrapper<ContractOrder> lock = new QueryWrapper<>();
//		lock.lambda()
//				.eq(ContractOrder::getId, 1)
//				.last("for update");
//		ContractOrder order = contractOrderMapper.selectOne(lock);

		// 开启全局事务
		SeataUtils.startGlobalTx();
		// 修改本地数据
//		UpdateWrapper<ContractOrder> update = new UpdateWrapper<>();
//		update.lambda()
//				.set(ContractOrder::getStatus, "4")
//				.eq(ContractOrder::getId, 1);
//		contractOrderMapper.update(null, update);

		// 调用其它服务
		PigxUser user = SecurityUtil.getLoginUser();

		WalletContractAccountDetailEnum.OperationType enums;
		if ("1".equals(operationType)) {
			enums = WalletContractAccountDetailEnum.OperationType.FREEZE;
		} else if ("2".equals(operationType)) {
			enums = WalletContractAccountDetailEnum.OperationType.UN_FREEZE;
		} else {
			throw new RuntimeException();
		}


		WalletContractAccountChangeDTO param = new WalletContractAccountChangeDTO();
		param.setUserId(user.getId());
		param.setAmount(BigDecimal.ONE);
		param.setSymbolId(1L);
		param.setSymbolName("BTCUSDT");
		param.setBusinessType(WalletContractAccountDetailEnum.BusinessType.POSITION_MARGIN);
		param.setOperationType(enums);

		List<WalletContractAccountChangeDTO> paramList = Arrays.asList(param);
		R r = walletContractAccountControllerFeign.change(paramList, SecurityConstants.FROM_IN);

		// 重新查询
//		ContractOrder newOrder = contractOrderMapper.selectById(1);
//		System.err.println(newOrder.getStatus());

		// 回滚
		SeataUtils.rollbackGlobalTx();
		mysqlUtil.rollbackLocalTx();

		// 提交全局事务
//		SeataUtils.commitGlobalTx();
		// 提交本地事务
//		commonUtil.commitLocalTx();

		System.err.println(123);
	}

	/**
	 * 合约账户详情
	 *
	 * @param userId 用户id
	 * @return {@link ContractAccount}
	 * @since 2022/9/22 10:12
	 */
	@Override
	public ContractAccount accountInfo(Long userId) {
		mysqlUtil.startLocalTx();
		SeataUtils.startGlobalTx();
		ContractAccount info = contractUtils.getContractAccountByUserId(userId);

		SeataUtils.rollbackGlobalTx();
		mysqlUtil.rollbackLocalTx();
		return info;
	}

	/**
	 * 合约账户详情并锁定
	 *
	 * @param userId 用户id
	 * @return {@link ContractAccount}
	 * @since 2022/9/22 10:12
	 */
	@Override
//	@GlobalTransactional
	public ContractAccount accountInfoLock(Long userId) {
		mysqlUtil.startLocalTx();
		SeataUtils.startGlobalTx();
		ContractAccount info = contractUtils.getContractAccountByUserIdLock(userId);

		SeataUtils.rollbackGlobalTx();
		mysqlUtil.rollbackLocalTx();
		return info;
	}
}
